package org.ydsw.data.manage.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;

/**
 * <p>Title: utils </p>
 * <p>Description：
 * 请求返回数据处理
 * </p>
 * @author admin
 */
@Data
public class JsonResponse implements Serializable {

	/**
	 * @fieldName: serialVersionUID
	 * @fieldType: long
	 * @Description: 序列号
	 */
	private static final long serialVersionUID = -7941354869234545831L;
	// 默认请求成功
	public static final int SUCCESS_CODE = 0;
	public static final String SUCCESS_MESSAGE = "请求成功";

	public static final int FAIL_CODE = 500;
	public static final int FAIL_CODE_2 = 400;
	public static final String FAIL_MESSAGE = "请求失败";

	// code码
	private int code;
	// 返回的信息
	private String msg;
	// 返回的数据
	private Object data;

	/**
	 * @Title:JsonResponse
	 * @Description:JsonResponse
	 * @param code 返回的code码
	 * @param msg 返回的信息
	 * @param data 返回的数据
	 */
	private JsonResponse(int code, String msg, Object data) {
		this.code = code;
		this.msg = msg;
		if (null != data) {
			this.data = data;
		} else {
			this.data = new HashMap<String, Object>();
		}
	}

	/**
	 * @Title: respSuccess
	 * @Description: 返回成功
	 * @param data 返回的数据
	 * @return: JsonResponse

	 */
	public static JsonResponse respSuccess(Object data) {
		return new JsonResponse(SUCCESS_CODE, SUCCESS_MESSAGE, data);
	}

	/**
	 * @Title: respSuccess
	 * @Description: 返回成功
	 * @return: JsonResponse

	 */
	public static JsonResponse respSuccess() {
		return new JsonResponse(SUCCESS_CODE, SUCCESS_MESSAGE, null);
	}

	/**
	 * @Title: respSuccess
	 * @Description: 返回成功
	 * @param msg 返回的信息
	 * @param data 返回的数据
	 * @return: JsonResponse
	 */
	public static JsonResponse respSuccess(String msg, Object data) {
		return new JsonResponse(SUCCESS_CODE, msg, data);
	}

	/**
	 * @Title: respSuccess
	 * @Description: 返回成功
	 * @param code 返回的code码
	 * @param msg 返回的信息
	 * @param data 返回的数据
	 */
	public static JsonResponse respSuccess(int code, String msg, Object data) {
		return new JsonResponse(code, msg, data);
	}

	/**
	 * @Title: responseFail
	 * @Description: 失败不带参数
	 * @param code
	 * @param message
	 * @return
	 */
	public static JsonResponse responseFail(int code, String message) {
		return new JsonResponse(code, message, null);
	}

	/**
	 * @param message
	 * @Description: responseFail
	 */
	public static JsonResponse responseFail(String message) {
		return new JsonResponse(FAIL_CODE, FAIL_MESSAGE, message);
	}

	/**
	 * @param message
	 * @Description: responseFail2
	 */
	public static JsonResponse responseFail2(String message) {
		return new JsonResponse(FAIL_CODE_2, FAIL_MESSAGE, message);
	}

	/**
	 * @Description: toJsonString
	 * @return: java.lang.String
	 */
	public String toJsonString() {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("code", code);
		jsonObject.put("msg", msg);
		jsonObject.put("data", data);
		return jsonObject.toJSONString();
	}

}
